<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<!-- 

    工厂模式创建对象：工厂模式是JAvaScript中的一种设计模式

 -->

<body>
    <script>
        function createStudent(name, age, gender, subject) {
            var student = {};
            student.name = name;
            student.age = age;
            student.gender = gender;
            student.subject = subject;
            student.speak = function() {
                console.log('hello');
            }
            return student;
        }

        var s1 = createStudent('张三', 10, '男', '数学');
        var s2 = createStudent('李四', 40, '男', '语文');
        console.log(s1);
        console.log(s2);



        //工厂模式的 简化版本
        function createStudent(name, age, gender, subject) {
            return {
                name: name,
                age: age,
                gender: gender,
                subject: subject,
                speak: function() {
                    console.log('hello')

                }
            }
        }

        var s1 = createStudent('张三', 10, '男', '数学');
        var s2 = createStudent('李四', 40, '男', '语文');
        console.log(s1);
        console.log(s2);
    </script>
</body>

</html>